/*
 * Copyright (c) Huawei Device Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "session.h"

#include <gtest/gtest.h>
#include <memory>

#include "channel.h"
#include "ipc_object_stub.h"
#include "loghelper.h"
#include "reader.h"
#include "se_service.h"
#include "service_manager.h"
#include "access_control_enforcer.h"
#include "secure_element_agent.h"
#include "access_rule_cache.h"
#include "se_common_exception.h"

namespace OHOS {
namespace SE {
namespace TEST {
using namespace testing::ext;
using namespace OHOS::SE;

class AccessRuleCacheTest : public testing::Test {
public:
    static void SetUpTestCase() {};
    static void TearDownTestCase() {};
    void SetUp();
    void TearDown();
public:
    std::shared_ptr<AccessRuleCache> ruleCache_;
    std::shared_ptr<SecureElementAgent> agent_;
    std::weak_ptr<SecureElementAgent> weakAgent_;
};

void AccessRuleCacheTest::SetUp()
{
    InfoLog("AccessRuleCacheTest::SetUp begin!");
    agent_ = std::make_shared<SecureElementAgent>("eSE");
    weakAgent_ = agent_;
    ruleCache_ = std::make_shared<AccessRuleCache>(true, true, agent_);
    InfoLog("AccessRuleCacheTest::SetUp done!");
}

void AccessRuleCacheTest::TearDown()
{
    InfoLog("AccessRuleCacheTest::TearDown begin!");
    InfoLog("AccessRuleCacheTest::TearDown done!");
}

/**
 * @tc.name: Initialize_Test001
 * @tc.desc: Test Initialize.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleCacheTest, Initialize_Test001, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> agent;
    std::shared_ptr<AccessRuleCache> accessRuleCache= std::make_shared<AccessRuleCache>(true, true, agent);
    bool statusCode = accessRuleCache->Initialize();
    EXPECT_TRUE(statusCode);
}

/**
 * @tc.name: Initialize_Test002
 * @tc.desc: Test Initialize.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleCacheTest, Initialize_Test002, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> agent;
    std::shared_ptr<AccessRuleCache> accessRuleCache= std::make_shared<AccessRuleCache>(false, true, agent);
    bool statusCode = accessRuleCache->Initialize();
    EXPECT_TRUE(statusCode);
}

/**
 * @tc.name: Initialize_Test003
 * @tc.desc: Test Initialize.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleCacheTest, Initialize_Test003, TestSize.Level1)
{
    try {
        std::shared_ptr<SecureElementAgent> agent = std::make_shared<SecureElementAgent>("eSE");
        agent->Initialize();
        agent->OnStateChange(true);
        std::shared_ptr<AccessRuleCache> accessRuleCache= std::make_shared<AccessRuleCache>(true, true, agent);
        bool statusCode = accessRuleCache->Initialize();
        EXPECT_FALSE(statusCode);
    } catch (const NoSuchElementError &e) {
        ErrorLog("exception happens.");
    } catch (const UnsupportedOperationError &e) {
        ErrorLog("exception happens.");
    }
}

/**
 * @tc.name: Initialize_Test004
 * @tc.desc: Test Initialize.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleCacheTest, Initialize_Test004, TestSize.Level1)
{
    std::shared_ptr<SecureElementAgent> agent = std::make_shared<SecureElementAgent>("eSE");
    agent->Initialize();
    agent->OnStateChange(true);
    std::shared_ptr<AccessRuleCache> accessRuleCache= std::make_shared<AccessRuleCache>(true, false, agent);
    accessRuleCache->Initialize();
    accessRuleCache->Initialize();
    EXPECT_TRUE(accessRuleCache != nullptr);
}

/**
 * @tc.name: IsAllowed_Test001
 * @tc.desc: Test IsAllowed.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleCacheTest, IsAllowed_Test001, TestSize.Level1)
{
    if (ruleCache_ != nullptr) {
        std::string appId = "";
        std::string fingerPrint = "";
        std::vector<uint8_t> aid = {0xA0, 0x00, 0x00, 0x01, 0x51, 0x00, 0x00, 0x00};
        bool statusCode = ruleCache_->IsAllowed(aid, appId, fingerPrint);
        EXPECT_FALSE(statusCode);
    }
}

/**
 * @tc.name: IsAllowed_Test002
 * @tc.desc: Test IsAllowed.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleCacheTest, IsAllowed_Test002, TestSize.Level1)
{
    if (ruleCache_ != nullptr) {
        std::string appId = "123";
        std::string fingerPrint = "123";
        std::vector<uint8_t> aid = {0x01, 0x02, 0x03};
        bool statusCode = ruleCache_->IsAllowed(aid, appId, fingerPrint);
        EXPECT_FALSE(statusCode);
    }
}

/**
 * @tc.name: IsClosed_Test001
 * @tc.desc: Test Session IsClosed.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleCacheTest, IsAllowedByFingerPrint_Test001, TestSize.Level1)
{
    if (ruleCache_ != nullptr) {
        const std::string fingerPrint = "";
        bool statusCode = ruleCache_->IsAllowedByFingerPrint(fingerPrint);
        EXPECT_FALSE(statusCode);
    }
}

} // TEST
} // SE
} // OHOS